#ifndef __HEADER_H
#define __HEADER_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> 
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <netinet/in_systm.h>
#include <netpacket/packet.h>
#include <memory.h>
#include <math.h>

#define MTU_TO_CCU			0x3
#define CCU_TO_MTU			0x4

#define FRAME_CTRL			0x1//控制帧
#define FRAME_DATA			0x2//数据帧

#define MTU_ID				0x1
#define RCU_ID				0x1

#define LOCAL_CTRL			0//本地控制
#define RMT_CTRL			0x1//遥控
#define LONG_RNG			0x2//远程控制
#define AUTO_CTRL			0x3//自动控制

#define FWD_RUN				0x1//前进
#define STOP_RUN			0x2//停车
#define BCK_RUN				0x3//后退

#define CAN_INV_MASK		0x1ff0fff0
#define CAN_MV_MASK			0x1ff0ffff
#define CAN_LOCO_MASK		0x1ff0ffff

#pragma pack(1)

typedef  struct{
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	uint8_t mode;//机车选择的控制模式
	uint8_t resv[4];
}can_cmd_req_t;

typedef struct{
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	uint8_t status;//各设备与CCU连接状态
	uint8_t ctrl;//远程控制模式选择
	uint8_t gear;//档位
	uint8_t dev_ctrl;//机车外设控制
	uint8_t resv;
}can_cmd_res_t;

typedef struct{
	uint8_t type;//类型
	uint8_t cmd;//命令
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	uint8_t mode;//控制模式
	uint8_t resv[11];
	uint8_t crc;
}udp_cmd_req_t;

typedef struct{
	uint8_t type;//类型
	uint8_t cmd;//命令
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	uint8_t status;//各设备连接状态
	uint8_t ctrl;//远程控制模式选择
	uint8_t gear;//档位
	uint8_t f_light;//前灯控制
	uint8_t b_light;//后灯控制
	uint8_t alarm;//警灯控制
	uint8_t siren;//喇叭控制
	uint8_t reset;//复位控制
	uint8_t scram;//急停控制
	uint8_t resv[9];
	uint8_t crc;
}udp_cmd_res_t;

typedef struct{
	uint16_t bus_volt;//变频器母线电压
	uint16_t bus_curr;//变频器母线电流
	uint16_t out_volt;//变频器输出电压
	uint16_t out_curr;//变频器输出电流
	uint16_t out_power;//变频器输出功率
	uint16_t speed;//电机转速
	uint16_t torque;//电机转矩
	uint8_t out_freq;//电机运行频率
	uint8_t work_stat;//变频器工作状态
	uint8_t direction;//电机旋转方向
	uint8_t fault_level;//变频器故障等级
	uint8_t fault;//故障代码
	uint8_t resv[5];
}inv_t;

typedef struct{
	uint16_t voltage;//总电压
	uint16_t current;//总电流
	uint16_t batt_volt;//启动电瓶电压
	uint16_t IR_value;//绝缘电阻值
	uint16_t max_volt;//单体最高电压
	uint16_t min_volt;//单体最低电压
	uint8_t SOC;//剩余电量
	uint8_t max_temp;//最高单体温度
	uint8_t min_temp;//最低单体温度
	uint8_t chg_stat;//充电状态
	uint8_t fault_level;//故障等级
	uint8_t fault;//故障代码
	uint8_t resv[5];
}mv_t;

typedef struct{
	uint16_t in_volt;//DCDC输入电压
	uint16_t out_volt;//DCDC输出电压
	uint8_t out_curr;//DCDC输出电流
	uint8_t temp;//DCDC散热器温度
	uint8_t work_stat;//DCDC工作状态
	uint8_t fault;//故障代码
}dcdc_t;

typedef struct{
	uint16_t torque[4];//制动力
	uint16_t pressure[4];//制动气压
	uint8_t resv[4];
}sample_t;

typedef struct{
	uint8_t mode;//控制模式
	uint8_t motor;//电机选择
	uint8_t speed;//车速
	uint8_t scram;//急停
	uint8_t gear_dir;//档位方向
	uint8_t gear;//档位
	uint8_t point;//点动
	uint8_t f_light;//前灯状态
	uint8_t b_light;//后灯状态
	uint8_t alarm;//警灯状态
	uint8_t siren;//喇叭状态
	uint8_t run_dir;//机车运行方向
	uint8_t fault_level;//故障等级
	uint8_t fault;//故障代码
	uint8_t resv[6];
}loco_t;

typedef struct{
	uint8_t ache;//原点
	uint16_t distance;//距离
}locate_t;

typedef struct{
	uint8_t type;//类型
	uint8_t cmd;//命令
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	uint8_t resv[3];
	mv_t mv;
	inv_t inv[2];
	dcdc_t dcdc;
	sample_t sample;
	loco_t loco;
	uint8_t crc;
}udp_data_req_t;

typedef struct{
	uint8_t type;//类型
	uint8_t cmd;//命令
	uint8_t seq;//序号
	uint8_t mtu_id;
	uint8_t rcu_id;
	locate_t locate;
	uint8_t resv[9];
	uint8_t crc;
}udp_data_res_t;

#pragma pack()

extern int sock_mtu_eth, sock_mtu_can;
void write_file(char *connect);

void *mtu_ask_ccu(void *arg);
void *ccu_res_mtu(void *arg);
void *mtu_data_up(void *arg);

#endif
